<?php
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use common\models\Article;
use common\models\Coach;
use common\models\Course;
use common\models\Signup;

/**
 * Site controller
 */
class SiteController extends Controller
{

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ]
        ];
    }

    /**
     * 首页
     */
    public function actionIndex()
    {
        $course = Course::find()->all();
        $news = Article::find()->where(['cid' => 5])->limit(9)->all();
        $model = new Signup();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
              var_dump('报名成功');
            }
        } 

        return $this->render('index', [
          'course' => $course,
          'news' => $news,
          'model' => $model
        ]);
    }

    public function actionLead(){
        return $this->render('lead');
    }

    /**
     * 单页列表页
     */
    public function actionPageList()
    {
        // 默认栏目页显示ID为2的文章
        $model = Article::findOne(2); 

        return $this->render('page_list', [
          'show_id' => 2,
          'model' => $model
        ]);
    }

    /**
     * 新闻列表页
     */
    public function actionList()
    {
        return $this->render('list');
    }

    /**
     * 教练列表页
     */
    public function actionCoachList()
    {
        // 获取教练分类，默认为3
        $pid = Yii::$app->request->get('pid', 3);

        $model = Coach::findAll(['pid' => $pid]);
        return $this->render('coach_list', [
          'pid' => $pid,
          'model' => $model
        ]);
    }

    public function actionShow($id)
    {
        $model = Article::findOne($id); 
        return $this->render('show', [
          'show_id' => $id,
          'model' => $model
        ]);
    }

}
